package com.user.service.impl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import org.springframework.stereotype.Service;

import com.user.dao.UserDao;
import com.user.dao.impl.UserDaoImpl;
import com.user.domain.User;
import com.user.service.UserInfoService;


public class UserInfoServiceImpl implements UserInfoService{

	private UserDao userDao = new UserDaoImpl();
	private User user = new User();
	private String builder_n;
	private java.io.InputStream inputStream;
	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	
	public InputStream GetUserEntity(String name,String password){
		user=new User();
		
		user = userDao.getUser("from User where tellnumber = '"+name+"' and password ='"+password+"'");
		if(user.getId()!=0){
			StringBuilder builder=new StringBuilder();
			builder.append('[');
			builder.append('{');
			builder.append("id:").append(user.getId()).append(',');
			builder.append("username:\"").append(user.getUsername()).append("\",");
			builder.append("tellnumber:\"").append(user.getTellnumber()).append("\",");
			builder.append("token:\"").append(userDao.getToken(user.getTellnumber())).append("\"");
			builder.append('}');
			builder.append(']');
			builder_n = builder.toString();
			inputStream = new ByteArrayInputStream(builder_n.getBytes());
		}else{
			StringBuilder builder=new StringBuilder();
			builder.append('[');
			builder.append('{');
			builder.append("id:").append("").append(',');
			builder.append("username:\"").append("").append("\",");
			builder.append("tellnumber:\"").append("").append("\"");
			builder.append('}');
			builder.append(']');
			builder_n = builder.toString();
			inputStream = new ByteArrayInputStream(builder_n.getBytes());
		}
		return inputStream;
	}
	
	public List<User> GetUsersEntity(String str){
		return userDao.getUsers(str);
	}

	@Override
	public InputStream saveUser(String name,String number,String password) {
		User s = new User();
		s = userDao.getUser("from User where tellnumber = '"+number+"'");
		if(s.getId()!=0){
			StringBuilder builder=new StringBuilder();
			builder.append('[');
			builder.append('{');
			builder.append("message:\"").append("User is already registered").append("\"");
			builder.append("},");
			builder.deleteCharAt(builder.length()-1);
			builder.append(']');
			builder_n = builder.toString();
			inputStream = new ByteArrayInputStream(builder_n.getBytes());
		}else{
			user.setUsername(name);
			user.setTellnumber(number);
			user.setPassword(password);
			boolean t = userDao.save(user);
			if(t){
				StringBuilder builder=new StringBuilder();
				builder.append('[');
				builder.append('{');
				builder.append("message:\"").append("success").append("\"");
				builder.append("},");
				builder.deleteCharAt(builder.length()-1);
				builder.append(']');
				builder_n = builder.toString();
				inputStream = new ByteArrayInputStream(builder_n.getBytes());
			}else{
				StringBuilder builder=new StringBuilder();
				builder.append('[');
				builder.append('{');
				builder.append("message:\"").append("fail").append("\"");
				builder.append("},");
				builder.deleteCharAt(builder.length()-1);
				builder.append(']');
				builder_n = builder.toString();
				inputStream = new ByteArrayInputStream(builder_n.getBytes());
			}
		}
		return inputStream;
	}

	@Override
	public InputStream setUpdate(String name, String password) {
		user = new User();
		user = userDao.getUser("from User where tellnumber = "+name+"");
		if(user.getId()!=0){
			boolean t = userDao.updateUser(name, password);
			if(t){
				StringBuilder builder=new StringBuilder();
				builder.append('[');
				builder.append('{');
				builder.append("message:\"").append("update success").append("\"");
				builder.append("},");
				builder.deleteCharAt(builder.length()-1);
				builder.append(']');
				builder_n = builder.toString();
				inputStream = new ByteArrayInputStream(builder_n.getBytes());
			}else{
				StringBuilder builder=new StringBuilder();
				builder.append('[');
				builder.append('{');
				builder.append("message:\"").append("update fail").append("\"");
				builder.append("},");
				builder.deleteCharAt(builder.length()-1);
				builder.append(']');
				builder_n = builder.toString();
				inputStream = new ByteArrayInputStream(builder_n.getBytes());
			}
		}else{
			StringBuilder builder=new StringBuilder();
			builder.append('[');
			builder.append('{');
			builder.append("message:\"").append("The user is not registered!").append("\"");
			builder.append("},");
			builder.deleteCharAt(builder.length()-1);
			builder.append(']');
			builder_n = builder.toString();
			inputStream = new ByteArrayInputStream(builder_n.getBytes());
		}
		return inputStream;
	}

	@Override
	public InputStream checkToken(String token) {
		boolean t = userDao.checkToken(token);
		if(t){
			StringBuilder builder=new StringBuilder();
			builder.append('[');
			builder.append('{');
			builder.append("message:\"").append("effectively").append("\"");
			builder.append("},");
			builder.deleteCharAt(builder.length()-1);
			builder.append(']');
			builder_n = builder.toString();
			inputStream = new ByteArrayInputStream(builder_n.getBytes());
		}else{
			StringBuilder builder=new StringBuilder();
			builder.append('[');
			builder.append('{');
			builder.append("message:\"").append("ineffectively").append("\"");
			builder.append("},");
			builder.deleteCharAt(builder.length()-1);
			builder.append(']');
			builder_n = builder.toString();
			inputStream = new ByteArrayInputStream(builder_n.getBytes());
		}
		return inputStream;
	}

	
}
